/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Graph;

/**
 *
 * @author oliviersputael
 */
public class Node {
	public Node () {
		instruction = null;
		color = null;
	}
	
	public Node (Instructions instr, Color col) {
		instruction = instr;
		color = col;
	}
	
	public Node (Node n) {
		Instructions i = n.getInstruction();
		instruction = new Instructions(i.isAtomic(), i.getLabel(), i.getVariables(), i.getPosition(), i.getNext());
		color = n.getColor();
	}
	
	public Instructions getInstruction () {
		return instruction;
	}
	
	public Color getColor () {
		return color;
	}
	
	public void setColor (Color c) {
		color = c;
	}
	
	public int getConstraint () {
		int constraint = -1;
		for (int i = 0; i < instruction.getVariables().size() && constraint == -1; i++)
			constraint = instruction.getVarFromId(i).getConstraint();
		
		return constraint;
	}
	
	public boolean isColored () {
		return (!color.getName().equals("White"));
	}
	
	public boolean mustBeLinkedTo (Node n) {
		boolean result = false;
		
		// Use the same var
		for (int i = 0; i < instruction.getVariables().size() && !result; i++) {
			for (int j = 0; j < n.getInstruction().getVariables().size() && !result; j++) {
				result = instruction.getVarFromId(i).equals(n.getInstruction().getVarFromId(j));
			}
		}
		
		// Next in WHEN
		if (!result) {
			if (instruction.getPosition().substring(0, 4).equals("WHEN") && n.getInstruction().getNext() != null) {
				for (int i = 0; i < n.getInstruction().getNext().size() && !result; i++)
					result = instruction.equals(n.getInstruction().getNextFromID(i));
			}
		}
		
		// Next of IF condition, in a WHEN
		if (!result) {
			if (n.getInstruction().getLabel().substring(0, 2).equals("IF") && n.getInstruction().getPosition().substring(0, 4).equals("WHEN"))
				for (int i = 0; i < n.getInstruction().getNext().size() && !result; i++)
					result = instruction.equals(n.getInstruction().getNextFromID(i));
		}
		
		
		// METHOD Call
		if (!result) {
			if (instruction.getLabel().substring(0, 6).equals("METHOD") && n.getInstruction().getLabel().substring(0, 4).equals("CALL")) {
				int endI = instruction.getLabel().indexOf("(");
				int endN = n.getInstruction().getLabel().indexOf("(");
				result = instruction.getLabel().substring(7, endI).equals(n.getInstruction().getLabel().substring(5, endN));
			}
		}
		
		return result;
	}
	
	@Override
	public boolean equals (Object o) {
		Node n = (Node) o;
		boolean result = true;
		result = result && !(o == null);
		result = result && instruction.equals(n.getInstruction());
		
		return result;
	}
	
	@Override
	public String toString () {
		String toString = "";
		
		toString = toString + instruction.getLabel() + "\n";
		
		/*toString = toString + "NODE : \n";
		toString = toString + "    > label        : " + instruction.getLabel() + "\n";
		toString = toString + "    > position     : " + instruction.getPosition() + "\n";
		toString = toString + "    > color        : " + color.getName() + "\n";
		if (this.isConstrained()) {
			toString = toString + "    > constraint   : YES\n";
		}
		else {
			toString = toString + "    > constraint   : NO\n";
		}*/
		
		return toString;
	}
	
	public boolean isConstrained () {
		boolean result = false;
		
		for (int i = 0; i < instruction.getVariables().size() && !result; i++) {
			result = (instruction.getVarFromId(i).getConstraint() != -1);
		}
		
		return result;
	}
	
	private Instructions instruction;
	private Color color;
}
